package mvc.command.armory
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;

	import mvc.control.ArmoryEvent;

	import mx.collections.ArrayCollection;

	import nz.co.codec.flexorm.EntityManager;
	import nz.co.codec.flexorm.criteria.Criteria;

	import orm.fantasy.RCharItemVO;
	import orm.fantasy.TblCharacterVO;
	import orm.fantasy.TblItemPropertyVO;
	import orm.fantasy.TblItemVO;

	import view.design.DesignCharacter;
	import view.design.DesignSlot;

	public class EquipAllSlotsCmd implements ICommand
	{
		public function execute(event:CairngormEvent):void
		{
			var dsnCharacter:DesignCharacter = event.data.dsnCharacter as DesignCharacter;
			var tblCharacterVO:TblCharacterVO = (event as ArmoryEvent).tblCharacterVO;
			var em:EntityManager = EntityManager.instance;

			var criteria:Criteria = em.createCriteria(RCharItemVO);
			criteria.addEqualsCondition("tbl_character_id", tblCharacterVO.id);
			var rCollection:ArrayCollection = em.fetchCriteria(criteria);

			for each(var designSlot:DesignSlot in dsnCharacter.designSlots)
			{
				designSlot.tblCharacterVO = tblCharacterVO;
			}
			for each(var r:RCharItemVO in rCollection)
			{
				// 获取数据
				var tblItemVO:TblItemVO = em.fetchCriteriaFirstResult(
					em.createCriteria(TblItemVO).addEqualsCondition("id", r.tbl_item_id)) as TblItemVO;
				var tblItemPropVO:TblItemPropertyVO = em.fetchCriteriaFirstResult(
					em.createCriteria(TblItemPropertyVO).addEqualsCondition("info_item_id", tblItemVO.item_id)) as TblItemPropertyVO;
				// 查找并更新对应Slot
				for each(designSlot in dsnCharacter.designSlots)
				{
					if(new ArrayCollection(designSlot.availableSlots).contains(tblItemVO.slot))
					{
						designSlot.initilaizeArmory(r, tblItemVO, tblItemPropVO);
						break;
					}
				}
			}
		}
	}
}